<template>
  <div class="x-bar" :id="chartId" :option="option"></div>
</template>

<script>
import { formatNum } from "../util/tool";
export default {
  props: {
    chartId: {
      type: String
    },
    options: {
      type: Object
    }
  },
  data() {
    return {
      chart: "",
      option: {
        color: ["#00bffe"],
        // tooltip: {
        //   trigger: "axis",
        //   axisPointer: {
        //     type: "shadow"
        //   }
        // },
        grid: {
          top: '13%',
          left: "2%",
          right: "7%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [
          {
            name: '时间',
            type: "category",
            nameTextStyle: {
              padding: [0,0,-25,-5],
              color: '#cbcbcb'
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              color: '#cbcbcb'
            },
            axisLine: {
              lineStyle: {
                color: '#006893'
              }
            },
            data: this.options.xData,
          }
        ],
        yAxis: [
          {
            name: '数量',
            type: "value",
            nameTextStyle: {
              padding: [0,0,0,-30],
              color: '#cbcbcb'
            },
            axisLabel: {
              color: '#cbcbcb'
            },
            axisLine: {
              lineStyle: {
                color: '#006893'
              }
            },
            splitLine: {
              show: false
            }
          }
        ],
        series: [
          {
            type: "bar",
            barWidth: "60%",
            label: {
              normal: {
                show: true,
                position: 'top',
                color: '#fff'
              }
            },
            data: this.options.sData
          }
        ]
      }
    };
  },
  watch: {
    options: {
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.option.xAxis[0].data = newVal.xData;
            this.option.series[0].data = newVal.sData;
            this.chart.setOption(this.option);
          } else {
            this.chart.setOption(this.option);
          }
        } else {
          this.init();
        }
      },
      deep: true
    }
  },
  methods: {
    init() {
      this.chart = this.$echarts.init(document.getElementById(this.chartId));
      this.chart.setOption(this.option);
      window.addEventListener("resize", this.chart.resize);
    }
  },
  mounted() {
    this.init();
  }
};
</script>

<style lang="stylus" scoped>
.x-bar {
  width: 100%;
  height: 85%;
}
</style>
